﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlTypes;
using BPM.Foundation.WFDB;
using BPM.Base.Tools;
namespace BPM.Foundation.WFService
{
    public partial interface IWorkflowService
    {
        [System.ServiceModel.OperationContract]
        int? insertWFMessage(string pid, WF_Message wfMessage);

        [System.ServiceModel.OperationContract]
        int? deleteWFMessage(string pid, Guid id);

        [System.ServiceModel.OperationContract]
        int? setWFMessageState(string pid, WF_Message wfMessage);

        [System.ServiceModel.OperationContract]
        List<WF_Message_V> getSendWFMessage(string sendDepartmentNo,
                                            string sendDutyNo,
                                            string sendUserNo
                                            );
        [System.ServiceModel.OperationContract]
        List<WF_Message_V> getInceptWFMessage(string inceptDepartmentNo,
                                            string inceptDutyNo,
                                            string inceptUserNo
                                            );
        [System.ServiceModel.OperationContract]
        WF_Message getWFMessage(string pid, Guid guid);




    }
    public partial class WorkflowService : IWorkflowService
    {
        //10.1	√消息添加
        public int? insertWFMessage(string pid, WF_Message wfMessage)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                //[1]
                if (wfMessage == null)
                {
                    return 1;
                }
                //--

                //[2]
                wfMessage.ID = Guid.NewGuid();
                //--


                //[3]
                wfMessage.sendTime = DateTime.Now;
                //--

                //[4]
                wfMessage.firstSeeTime = new DateTime(1900, 1, 1);
                //--

                //[5]
                wfMessage.state = "没看";
                //-

                //[6]
                if(string.IsNullOrEmpty( wfMessage.inceptDepartmentNo)||wfMessage.inceptDepartmentNo=="none")
                {
                    wfMessage.inceptDepartmentNo = "";
                }

                //[7]
                if(string.IsNullOrEmpty(wfMessage.inceptDutyNo) || wfMessage.inceptDutyNo=="none")
                {
                    wfMessage.inceptDutyNo = "";
                }

                //[8]
                if(string.IsNullOrEmpty(wfMessage.inceptUserNo) || wfMessage.inceptUserNo=="none")
                {
                    wfMessage.inceptUserNo = "";
                }
                
                //[9]
                if(string.IsNullOrEmpty(wfMessage.sendDepartmentNo)||wfMessage.sendDepartmentNo=="none")
                {
                    wfMessage.sendDepartmentNo = "";
                }

                //[10]
                if(string.IsNullOrEmpty(wfMessage.sendDutyNo)|| wfMessage.sendDutyNo=="none")
                {
                    wfMessage.sendDutyNo = "";
                }

                //[11]
                if(string.IsNullOrEmpty(wfMessage.sendUserNo)||wfMessage.sendUserNo=="none")
                {
                    wfMessage.sendUserNo = "";
                }

                //[12]
                if(string.IsNullOrEmpty(wfMessage.content1))
                {
                    wfMessage.content1 = "";
                }

                //[13]
                if(string.IsNullOrEmpty(wfMessage.content2))
                {
                    wfMessage.content2 = "";
                }

                //[14]
                if(string.IsNullOrEmpty(wfMessage.content3))
                {
                    wfMessage.content3 = "";
                }

                //[15]
                if(string.IsNullOrEmpty(wfMessage.content4))
                {
                    wfMessage.content4 = "";
                }


                //[16]
                if(string.IsNullOrEmpty(wfMessage.head))
                {
                    wfMessage.head = "";
                }
              
                //[17]
                if(wfMessage.isNeedRevert==null )
                {
                wfMessage.isNeedRevert = 0;
                }

                //[18]
                if(wfMessage.parentId==null)
                {
                  wfMessage.parentId = Guid.Empty;
                }

                if(string.IsNullOrEmpty(wfMessage.revertState))
                {
                    wfMessage.revertState = "";
                }


                //[19]
                if (wfMessage.revertTime == null)
                {
                    wfMessage.revertTime = new DateTime(1900, 1, 1);
                }

                //[20]
                if(string.IsNullOrEmpty(wfMessage.secrecy))
                {
                    wfMessage.secrecy = "";
                }

                //[21]
                if(string.IsNullOrEmpty( wfMessage.type))
                {
                    wfMessage.type = "";
                }

                //[22]
                if(string.IsNullOrEmpty(wfMessage.urgency))
                {
                    wfMessage.urgency = "";
                }

                wfDB.WF_Message.InsertOnSubmit(wfMessage);
                try
                {
                    wfDB.SubmitChanges();
                }
                catch (System.Exception ex)
                {
                    MyLog.writeError(ex.Message);
                }
                return null;
            }
        }

        //10.2	√消息删除
        public int? deleteWFMessage(string pid, Guid id)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                //[1.1]
                var result = wfDB.WF_Message.SingleOrDefault(p => p.ID == id);
                if (result == null)
                {
                    return 1;
                }
                //--

                //[1.2]
                wfDB.WF_Message.DeleteOnSubmit(result);
                try
                {
                    wfDB.SubmitChanges();
                }
                catch (System.Exception ex)
                {
                    MyLog.writeError(ex.Message);
                }
                return null;
                //--
            }

        }

        //10.3	√设置消息状态
        public int? setWFMessageState(string pid, WF_Message wfMessage)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                //[1.1]
                var result = wfDB.WF_Message.SingleOrDefault(p => p.ID == wfMessage.ID);
                if (result == null)
                {
                    return 1;
                }
                //--

                //[1.2]
                result.state = "已看";
                //--

                //[1.3]
                result.firstSeeTime = DateTime.Now;
                //--

                try
                {
                    wfDB.SubmitChanges();
                }
                catch (System.Exception ex)
                {
                    MyLog.writeError(ex.Message);
                }
                return null;
            }
        }


        //10.4	√查询发送消息
        public List<WF_Message_V> getSendWFMessage(string sendDepartmentNo, string sendDutyNo, string sendUserNo)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                //[1.1]
                if (string.IsNullOrEmpty(sendDepartmentNo) && string.IsNullOrEmpty(sendDutyNo) && string.IsNullOrEmpty(sendUserNo))
                {
                    return wfDB.WF_Message_V.ToList();
                }
               
                //--

                //[1.2]
                if (string.IsNullOrEmpty(sendDutyNo) && string.IsNullOrEmpty(sendUserNo))
                {
                    return wfDB.WF_Message_V.Where(p => p.sendDepartmentNo == sendDepartmentNo).ToList();
                }

                //--

                //[1.3]
                if (string.IsNullOrEmpty(sendDepartmentNo) && string.IsNullOrEmpty(sendUserNo))
                {
                    return wfDB.WF_Message_V.Where(p => p.sendDutyNo == sendDutyNo).ToList();
                }

                //--

                //[1.4]
                if (string.IsNullOrEmpty(sendDepartmentNo) && string.IsNullOrEmpty(sendDutyNo))
                {
                    return wfDB.WF_Message_V.Where(p => p.sendUserNo == sendUserNo).ToList();
                }

                //--

                //[1.5]
                if (!string.IsNullOrEmpty(sendDepartmentNo) && !string.IsNullOrEmpty(sendDutyNo) && string.IsNullOrEmpty(sendUserNo))
                {
                    return wfDB.WF_Message_V.Where(p => p.sendDepartmentNo == sendDepartmentNo && p.sendDutyNo == sendDutyNo).ToList();
                }

                //--
                //[1.6]
                if (!string.IsNullOrEmpty(sendDepartmentNo) && !string.IsNullOrEmpty(sendDutyNo) && !string.IsNullOrEmpty(sendUserNo))
                {
                    return wfDB.WF_Message_V.Where(
                        p => p.sendDepartmentNo == sendDepartmentNo
                            &&
                            p.sendDutyNo == sendDutyNo
                            &&
                            p.sendUserNo == sendUserNo
                        ).ToList();
                }
                return null;

                //--
            }


        }

        //10.5	√查询接收消息
        public List<WF_Message_V> getInceptWFMessage(string inceptDepartmentNo, string inceptDutyNo, string inceptUserNo)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                //[1.1]
                if (string.IsNullOrEmpty(inceptDepartmentNo) && string.IsNullOrEmpty(inceptDutyNo) && string.IsNullOrEmpty(inceptUserNo))
                {
                    return wfDB.WF_Message_V.ToList();
                }

                //--

                //[1.2]
                if (string.IsNullOrEmpty(inceptDutyNo) && string.IsNullOrEmpty(inceptUserNo))
                {
                    return wfDB.WF_Message_V.Where(p => p.inceptDepartmentNo == inceptDepartmentNo).ToList();
                }

                //--

                //[1.3]
                if (string.IsNullOrEmpty(inceptDepartmentNo) && string.IsNullOrEmpty(inceptUserNo))
                {
                    return wfDB.WF_Message_V.Where(p => p.inceptDutyNo == inceptDutyNo).ToList();
                }

                //--

                //[1.4]
                if (string.IsNullOrEmpty(inceptDepartmentNo) && string.IsNullOrEmpty(inceptDutyNo))
                {
                    return wfDB.WF_Message_V.Where(p => p.inceptUserNo == inceptUserNo).ToList();
                }

                //--

                //[1.5]
                if (!string.IsNullOrEmpty(inceptDepartmentNo) && !string.IsNullOrEmpty(inceptDutyNo) && string.IsNullOrEmpty(inceptUserNo))
                {
                    return wfDB.WF_Message_V.Where(p => p.inceptDepartmentNo == inceptDepartmentNo && p.inceptDutyNo == inceptDutyNo).ToList();
                }

                //--
                //[1.6]
                if (!string.IsNullOrEmpty(inceptDepartmentNo) && !string.IsNullOrEmpty(inceptDutyNo) && !string.IsNullOrEmpty(inceptUserNo))
                {
                    return wfDB.WF_Message_V.Where(
                        p => p.inceptDepartmentNo == inceptDepartmentNo
                            &&
                            p.inceptDutyNo == inceptDutyNo
                            &&
                            p.inceptUserNo == inceptUserNo
                        ).ToList();
                }
                return null;

                //--
            }

        }

        //10.6	√得到消息
        public WF_Message getWFMessage(string pid, Guid guid)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                //[1.1]
                if (guid == Guid.Empty)
                {
                    return null;
                }
                //--

                //[1.2]
                return wfDB.WF_Message.SingleOrDefault(p => p.ID == guid);
            }
        }

    }
}
